/**
 * @file tsk_op.c
 * @synopsis  
 * @author wen wjcapple@163.com
 * @version 1.0
 * @date 2017-08-07
 */

#include "hb_app.h"
#include "tsk_op.h"


#ifdef __cplusplus
extern "C" {
#endif
int32_t deal_appoint_test(struct _tagUsrOtdrTest *pUsrTest,
		OtdrTestResult_t *pch_tr,
		_tagSpiDevCtrl *spi_ctrl,
		int (*stop)(void)
		)
{
	int32_t ret;
	struct glink_addr addr;
	struct tms_ack ack;
	memset(&ack, 0, sizeof(struct tms_ack));
	struct _tagCHPara usr_test_para;

	addr.dst = pUsrTest->src_addr.src;
	addr.src = ADDR_LOCAL;
	addr.pkid = pUsrTest->src_addr.pkid;
	ack.cmdid = pUsrTest->cmd;


	if(pUsrTest->state == USR_OTDR_TEST_WAIT)
	{
		sleep(1);
		pUsrTest->state = USR_OTDR_TEST_ACCUM;
		convert_para_host2algor(&pUsrTest->para,&usr_test_para);
		ret = get_ch_otdr_curv(pUsrTest->para.pipe, &usr_test_para, pch_tr,spi_ctrl,stop);
		if(ret == 0)
			ret_host_otdr_curv(pch_tr,pUsrTest);

		else
		{
			ack.errcode = CMD_RET_TEST_ERROR;
			hb_Ret_Ack_V2(&addr, &ack);
		}
		memset(pUsrTest, 0,sizeof(struct _tagUsrOtdrTest));
		barrier();
		pUsrTest->state = USR_OTDR_TEST_IDLE;
	}
	/*
	   else
	   {
	   ack.errcode = CMD_RET_EXIST_CMD ;
	   hb_Ret_Ack_V2(&addr, &ack);
	   }
	   */
	return 0;


}
int32_t refresh_ch_monitor_para(struct _tagCHFiberSec *pch_fiber_sec,
		struct _tagOtdrDev *potdr_dev)
{
	struct _tagHostOtdrPara HostPara;

	OtdrTestResult_t ch_tr;

	quick_lock(&pch_fiber_sec->lock);
	if(pch_fiber_sec->para.is_initialize)
	{
		convert_stdcfg_host2alarmodule(&pch_fiber_sec->para,&pch_fiber_sec->adpt_moudle);	

		memcpy(&HostPara.MeasureLength_m, &pch_fiber_sec->para.otdr_param.range,\
				sizeof(struct _tagHostOtdrPara) - 8);
		HostPara.slot = pch_fiber_sec->para.otdr_param.slot;
		HostPara.pipe = pch_fiber_sec->para.otdr_param.pipe;


		convert_para_host2algor(&HostPara, &potdr_dev->ch_para);
		potdr_dev->ch_ctrl.is_cfged = 1;
		//初始化用户配置的脉宽，和光纤长度
		potdr_dev->ch_ctrl.st_pulse = HostPara.PulseWidth_ns;
		potdr_dev->ch_ctrl.st_fiber_len = pch_fiber_sec->para.test_result.range;
		//初始化标准曲线的段，段损耗
		ch_tr.Ai = malloc(sizeof(float)*DATA_LEN);
		if(ch_tr.Ai == NULL)
			exit_self(errno,__FUNCTION__,__LINE__,"malloc ch_ti error");
		convert_tr_fbsec2algor(pch_fiber_sec,&ch_tr);
		init_sec_loss_len(&potdr_dev->ch_ctrl.sec_loss, &ch_tr, pch_fiber_sec);
		free(ch_tr.Ai);
		ch_tr.Ai = NULL;
	}
	else
		potdr_dev->ch_ctrl.is_cfged = 0;

	potdr_dev->ch_ctrl.refresh_para = 0;
	//初始化当前参数，测试次数
	potdr_dev->ch_ctrl.cur_fiber_len = 0;
	potdr_dev->ch_ctrl.test_count = 0;
	quick_unlock(&pch_fiber_sec->lock);

	return 0;


}

#ifdef __cplusplus
}
#endif

